//
//  LPCostomHeadLineAndTextViewAndTiShiView.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/7/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPCostomHeadLineAndTextViewAndTiShiView.h"
#import "CBTextView.h"
@interface LPCostomHeadLineAndTextViewAndTiShiView ()<UITextViewDelegate>
/**  白色视图  */
@property (strong, nonatomic) UIView  *backView;
/**  标题  */
@property (strong, nonatomic) UILabel  *headLine;
/**  输入框  */
@property (strong, nonatomic) CBTextView  *textView;
/**  取消按钮  */
@property (strong, nonatomic) UIButton  *cancleButton;
/**  确认按钮  */
@property (strong, nonatomic) UIButton  *sureButton;


@end
@implementation LPCostomHeadLineAndTextViewAndTiShiView

- (instancetype)initWithFrame:(CGRect)frame headLine:(NSString *)headLine
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor zj_colorWithR:20 G:20 B:20 Al:0.5];
        [self addSubview:self.backView];
        self.headLine.text = headLine;
        [self.backView addSubview:self.headLine];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.headLine.bottom - 1, self.width, 1)];
        view.backgroundColor = [UIColor viewBGGrayColor];
        [self.backView addSubview:view];
        [self.backView addSubview:self.textView];
        [self.backView addSubview:self.cancleButton];
        [self.backView addSubview:self.sureButton];
        
    }
    return self;
}


- (void)clickButton:(UIButton *)sender{
    if (sender.tag == 1) {
        if (self.textView.textView.text.length == 0) {
           [YJProgressHUD showSuccess:@"请输入请假原因" inview:self];
            return;
        }
        if (self.delegate) {
            [self.delegate LPCostomHeadLineAndTextViewAndTiShiWithString:self.textView.textView.text];
        }
    }else{
        if (self.delegate) {
            [self.delegate LPCostomHeadLineAndTextViewAndTiShiWithCancle:YES];
        }
    }
    [self removeFromSuperview];

}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    if (self.delegate) {
//        [self.delegate LPCostomHeadLineAndTextViewAndTiShiWithCancle:YES];
//    }
//    [self removeFromSuperview];
//
//}

- (UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith] * 0.86, 220 * [UIScreen zj_screenHScale])];
        _backView.center = self.center;
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.cornerRadius = 6;
        _backView.layer.masksToBounds = YES;
    }
    return _backView;
}

- (UILabel *)headLine{
    if (!_headLine) {
        _headLine = ({
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.backView.width, 40 * [UIScreen zj_screenHScale])];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = LYPFONT(17);
            label;
        });
    }
    return _headLine;
}

- (UIButton *)sureButton{
    if (!_sureButton) {
        _sureButton = ({
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.backView.width * 0.6 , self.textView.bottom + 20 * [UIScreen zj_screenHScale], self.backView.width * 0.5 * 0.6, 35 * [UIScreen zj_screenHScale])];
            [button setTitle:@"确认" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor themeColor];
            [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 5;
            button.tag = 1;
            button;
            
        });
    }
    return _sureButton;
}

- (UIButton *)cancleButton{
    if (!_cancleButton) {
        _cancleButton = ({
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.backView.width * 0.1 , self.textView.bottom + 20 * [UIScreen zj_screenHScale], self.backView.width * 0.5 * 0.6, 35 * [UIScreen zj_screenHScale])];
            [button setTitle:@"取消" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor whiteColor];
            [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 2;
            button.layer.borderColor = [UIColor themeColor].CGColor;
            button.layer.borderWidth = 1;
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 5;
            button;
            
        });
    }
    return _cancleButton;
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
        
    }
    
    NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (toBeString.length >= 200) {
        textView.text = [toBeString substringToIndex:200];
        return NO;
    }
    
    
    return YES;
    
}

- (CBTextView *)textView{
    if (!_textView) {
        _textView = [[CBTextView alloc] initWithFrame:CGRectMake(self.backView.width * 0.05, self.headLine.bottom + 10 * [UIScreen zj_screenHScale], self.backView.width * 0.9, 100 * [UIScreen zj_screenHScale]) leftSpace:10 * [UIScreen zj_screenWScale] rigSpace:10 * [UIScreen zj_screenWScale] topSpace:5 * [UIScreen zj_screenHScale] botSpace:2];
        _textView.layer.borderWidth = 1;
        _textView.layer.borderColor = [UIColor viewBGGrayColor].CGColor;
        _textView.layer.cornerRadius = 3;
        _textView.layer.masksToBounds = YES;
        _textView.textView.font = LYPFONT(15);
        _textView.placeHolder = @"请输入内容";
        _textView.textView.textColor = [UIColor textGrayColor];
        _textView.textView.returnKeyType = UIKeyboardTypeTwitter;
        _textView.aDelegate = self;
    }
    return _textView;
}


@end
